package client.comm.baoding.ui

import android.view.View
import androidx.core.view.children
import androidx.navigation.fragment.NavHostFragment
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.baoding.R
import client.comm.baoding.ui.vm.SetskxxViewModel
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.ActivitySetskxxBinding
import java.util.*
import client.comm.baoding.BaseMiddleActivity
class SetSkxxActivity : BaseMiddleActivity<SetskxxViewModel, ActivitySetskxxBinding>() {
    override fun getLayout(): Int {
        return R.layout.activity_setskxx
    }

    var timerTask: TimerTask? = null
    var timer: Timer? = null
    override fun initViewModel(): SetskxxViewModel = createViewModel()
    override fun onDestroy() {
        timerTask?.let { it.cancel() }
        timer?.let { it.cancel() }
        super.onDestroy()
    }

    override fun initView() {
        binding.event = this
        binding.tabWrap.getChildAt(0).isSelected = true
        binding.vm = viewModel

        StoreUtil.getBean<UserInfo>(this)?.let {
            viewModel.phone.value = it.phone
        }
    }

    override fun observerUI() {
        viewModel.codeRet.observe(this, {

            it?.apply {

                if (isSuccess) {
                    toast("获取验证码成功")

                    timerTask?.let { it.cancel() }
                    timer?.let { it.cancel() }

                    viewModel.getCodeEnable.postValue(false)

                    timerTask = object : TimerTask() {
                        var count = 60
                        override fun run() {
                            count--;
                            viewModel.getCodeTxt.postValue("${count}秒后获取")
                            if (count <= 0) {
                                viewModel.getCodeEnable.postValue(true)
                                viewModel.getCodeTxt.postValue("获取验证码")
                                timerTask!!.cancel()

                            }
                        }
                    }
                    timer = Timer().apply { schedule(timerTask, 0, 1000) }
                } else {
                    viewModel.getCodeEnable.value = true
                    viewModel.getCodeTxt.value = "获取验证码"

                }

            }
        })
    }

    fun tab(v: View) {
        if (v.isSelected)
            return
        for (c in binding.tabWrap.children) {
            if (c != v) {
                c.isSelected = false
            } else {
                v.isSelected = true
                when (v.tag) {
                    "0" -> {
//                        if (!(supportFragmentManager.findFragmentById(
//                                R.id.fragment
//                            ) as NavHostFragment).navController.popBackStack(
//                                R.id.oneTab,
//                                false
//                            )
//                        ) {
                        (supportFragmentManager.findFragmentById(
                            R.id.fragment
                        ) as NavHostFragment).navController.navigate(
                            R.id.oneTab
                        )
//                        }
                    }
                    "1" -> {
//                        if (!(supportFragmentManager.findFragmentById(
//                                R.id.fragment
//                            ) as NavHostFragment).navController.popBackStack(
//                                R.id.twoTab,
//                                false
//                            )
//                        ) {
                        (supportFragmentManager.findFragmentById(
                            R.id.fragment
                        ) as NavHostFragment).navController.navigate(
                            R.id.twoTab
                        )
//                        }
                    }
                    "2" -> {
//                        if (!(supportFragmentManager.findFragmentById(
//                                R.id.fragment
//                            ) as NavHostFragment).navController.popBackStack(
//                                R.id.threeTab,
//                                false
//                            )
//                        ) {
                        (supportFragmentManager.findFragmentById(
                            R.id.fragment
                        ) as NavHostFragment).navController.navigate(
                            R.id.threeTab
                        )
//                        }
                    }
                    "3" -> {
                        (supportFragmentManager.findFragmentById(
                            R.id.fragment
                        ) as NavHostFragment).navController.navigate(
                            R.id.fourTab
                        )
                    }
                }
            }
        }
    }
}